﻿@using OSBIDE.Library
@using OSBIDE.Library.Models
@using OSBIDE.Web.Models.ViewModels
@using OSBIDE.Library.Events
@using OSBIDE.Web.Models
@using System.Collections.Generic
@model List<AggregateFeedItem>
@{
    string additionalStyle = "";
    if (ViewBag.FeedItemStyle != null)
    {
        additionalStyle = ViewBag.FeedItemStyle;
    }
    OsbideUser currentUser = ViewBag.CurrentUser;
    Dictionary<int, int> subscriptions = new Dictionary<int, int>();
    foreach (EventLogSubscription log in currentUser.LogSubscriptions)
    {
        subscriptions[log.LogId] = log.LogId;
    }
    
}
@foreach (AggregateFeedItem item in Model)
{
    int lastLogId = item.Items.Select(m => m.LogId).Max();
    List<int> eventLogIds = item.Items.Select(i => i.LogId).ToList();
    bool isSubscribed = false;
    int[] ids = item.Items.Select(i => i.LogId).ToArray();
    string idString = string.Join(",", ids);
    foreach (int logId in eventLogIds)
    {
        if (subscriptions.ContainsKey(logId) == true)
        {
            isSubscribed = true;
            break;
        }
    }
    <section data-id="@lastLogId" class="feed-item-single @additionalStyle">

        <div class="feed-item-single-left">
            <img src="@Url.Action("Picture", "Profile", new { id = item.Creator.Id, size = 64 })" height="64" width="64" title="profile image" alt="profile image" />
            @if (item.HelpfulMarks > 0)
            {
                @:<span class="helpful-marks">+@item.HelpfulMarks</span>
            }
            @if (isSubscribed)
            {
                <img src="~/Content/icons/follow.png" alt="you are following this post" title="you are following this post" />
            }
        </div>
        <div class="feed-item-single-right">
            <h1><a href="@Url.Action("Index", "Profile", new { id = item.Creator.Id, component = OsbideVsComponent.UserProfile })">@item.Creator.FirstAndLastName</a> 
                <time 
                class="utc-time feed-item-date"
                datetime="@Helpers.DateAsUnixTime(item.MostRecentOccurance)" 
                data-original-date="@Helpers.RawDate(item.MostRecentOccurance)" 
                data-date-format="MM/DD/YYYY hh:mm A">
                @item.MostRecentOccurance.ToString("MM/dd @ hh:mmtt") (UTC)
            </time>
            </h1>
            <div>
                @{string partialName = string.Format("Feed/_{0}", item.Items.First().Event.EventName); }
                @Html.Partial(partialName, item)
            </div>
            <div class="feed-item-footer">
                <ul>
                    <li><a id="expand-comments-@lastLogId" class="anchor-pointer" onclick="expandComments('feed-item-comments-@lastLogId')">@item.Comments.Count Comment(s)</a></li>
                    <li><a href="@Url.Action("Details", "Feed", new { id = idString, component = OsbideVsComponent.FeedDetails })">Details</a></li>
                </ul>
            </div>
            @{long lastCommentPost = 0;
              if (item.Comments.LastOrDefault() != null)
              {
                  lastCommentPost = item.Comments.LastOrDefault().DatePosted.Ticks;
              }
              }
            <div class="feed-item-comments" id="feed-item-comments-@lastLogId" style="display:none;" data-lastPoleDate="@lastCommentPost">
                @foreach (LogComment comment in item.Comments)
                {
                    @Html.Partial("_CommentSingle", comment)
                }

            </div>
        </div>
        <div style="clear: both;">
            &nbsp;
        </div>
    </section>
}